<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');
die;
$sql="
    insert into ns_v1customer 
    SELECT
    cs.`e_oid`,
    cs.`trackId`,
    cs.`createdDate`,
    gmbe.`bridename`,
    gmbe.`brideaddrLn1`,
    gmbe.`brideaddrLn2`,
    gmbe.`bridephone`,
    gmbe.`bridemobile`,
    gmbe.`brideemail`,
    gmbe.`groomname`,
    gmbe.`groomaddrLn1`,
    gmbe.`groomaddrLn2`,
    gmbe.`groomphone`,
    gmbe.`groommobile`,
    gmbe.`groomemail`,
    cs.`note`,
    cs.`ceremony`,
    cs.`culture`,
    cs.`culture2`,
    cs.`reception`,
    cs.`source`,
    cs.`userAccount`,
    cs.`cancelled`,
    cs.`reminderNote`,
    cs.`cancelledDate`,
    cs.`cancelledOperator`,
    cs.`cancelledMessage`,
    cs.`admAppr`,
    cs.`is_ecn`
FROM
    (
        SELECT
            dd.`e_oid`,
            dd.`trackId`,
            dd.`createdDate`,
            dd.`note`,
            dd.`ceremony`,
            dd.`culture`,
            dd.`culture2`,
            dd.`reception`,
            dd.`source`,
            ee.account AS userAccount,
            dd.`cancelled`,
            dd.`reminderNote`,
            dd.`cancelledDate`,
            dd.`cancelledOperator`,
            dd.`cancelledMessage`,
            dd.`admAppr`,
            dd.`is_ecn`
        FROM
            ns_customer AS dd
        LEFT JOIN(
            SELECT
                aa.account,
                bb.oid_a AS customer_id
            FROM
                ns_user AS aa
            LEFT JOIN ns__ez_relation_customer_user AS bb ON aa.e_oid = bb.oid_b
        )AS ee ON dd.e_oid = ee.customer_id
    )AS cs,
    (
        SELECT
            gm.groomname,
            gm.groomaddrLn1,
            gm.groomaddrLn2,
            gm.groomphone,
            gm.groommobile,
            gm.groomemail,
            bm.bridename,
            bm.brideaddrLn1,
            bm.brideaddrLn2,
            bm.bridephone,
            bm.bridemobile,
            bm.brideemail,
            bm.customer_id
        FROM
            (
                SELECT
                    a.`name` AS groomname,
                    a.addrLn1 AS groomaddrLn1,
                    a.addrLn2 AS groomaddrLn2,
                    a.phone AS groomphone,
                    a.mobile AS groommobile,
                    a.email AS groomemail,
                    b.oid_a AS customer_id
                FROM
                    ns_customercontact AS a
                LEFT JOIN ns__ez_relation_customer_customercontact b ON a.e_oid = b.oid_b
                WHERE
                    b.var_a = 'groom'
            )AS gm,
            (
                SELECT
                    a.`name` AS bridename,
                    a.addrLn1 AS brideaddrLn1,
                    a.addrLn2 AS brideaddrLn2,
                    a.phone AS bridephone,
                    a.mobile AS bridemobile,
                    a.email AS brideemail,
                    b.oid_a AS customer_id
                FROM
                    ns_customercontact AS a
                LEFT JOIN ns__ez_relation_customer_customercontact b ON a.e_oid = b.oid_b
                WHERE
                    b.var_a = 'bride'
            )AS bm
        WHERE
            gm.customer_id = bm.customer_id
    )AS gmbe
WHERE
    cs.e_oid = gmbe.customer_id

    ";

sql_execute($sql);
